<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\association\Association;
use app\common\model\firm\Category;
use app\common\model\firm\Firm as FirmFirm;
use app\common\model\firm\Product;
use app\common\model\User;

/**
 * 企业接口
 */
class Firm extends Api
{
    protected $noNeedLogin = ['category', 'lists', 'info', 'products'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (企业分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $list = Category::all(['pid' => 0]);
        $this->success('请求成功', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (企业列表)
     * @ApiMethod (GET)
     * @ApiParams (name="first_id", type="integer", required=true, sample="", description="一级分类")
     * @ApiParams (name="two_id", type="integer", required=true, sample="", description="二级分类")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="关键字")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $model = new FirmFirm();
        if (isset($keywords) && $keywords) {
            $model->whereLike('title', $keywords);
        }
        if ($first_id && $two_id) {
            $model->where('category_id', $two_id);
        }
        if ($first_id && !$two_id) {
            $cate_ids = Category::whereIn('pid', $first_id)->column('id');
            $model->whereIn('category_id', $cate_ids);
        }
        $lists = $model->paginate();
        $this->success('请求成功', $lists);
    }

    /**
     * @Author LK
     * @ApiTitle (企业详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="企业id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $param = request()->param();
        extract($param);
        $data = FirmFirm::get($id);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (企业产品)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="企业ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function products()
    {
        $param = request()->param();
        extract($param);
        $lists = Product::where('f_id', $id)->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (产品详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="产品ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function products_info()
    {
        $param = request()->param();
        extract($param);
        $data = Product::get($id);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (企业员工)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */

    public function employee_list()
    {
        $param = request()->param();
        extract($param);
        $userInfo = User::get($this->auth->id);
        if (!$userInfo->firm) {
            $this->error('未成为企业管理员');
        }
        $lists = User::where('firm_id', $this->auth->id)->paginate();
        $this->success('', $lists);
    }

    /**
     * 搜索员工
     *
     * @ApiMethod (GET)
     * @param string $keywords  手机号
     */
    public function search_user()
    {
        $param = $this->request->param();
        extract($param);
        if (!isset($keywords) || !$keywords) {
            $this->error(__('参数错误'));
        }
        $data = User::where('mobile', $keywords)->where('firm_id', 0)->find();
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (设置员工角色)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="employee", type="string", required=true, sample="", description="员工类型:1=普通员工,2=联络员")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function set_employee()
    {
        $param = $this->request->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error(__('参数错误'));
        }
        if (!isset($employee) || !$employee) {
            $this->error(__('请选择类型'));
        }
        $user = User::get($id);
        if (!$user) {
            $this->error('手机号不存在');
        }
        $user->employee = $employee;
        $res = $user->save();
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
    /**
     * @Author LK
     * @ApiTitle (企业信息)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function firm_info()
    {
        $param = $this->request->param();
        extract($param);
        $data = Association::where([
            'user_id' => $this->auth->id,
            'state' => '2',
            'type' => '1',
        ])->order('examinetime desc')->find();
        $this->success('', $data);
    }
}
